package com.qyer.search.utils;/**
 * User: Z J Wu Date: 2016/01/06 Time: 17:23 Package: com.qyer.search.utils
 */

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class JsonOperator {

  private static final Logger LOGGER = LoggerFactory.getLogger(JsonOperator.class);

  private static class InnerHolder {

    private static final JsonOperator INSTANCE = new JsonOperator();
  }

  private JsonOperator() {
  }

  public static final JsonOperator getInstance() {
    return InnerHolder.INSTANCE;
  }

  private final ObjectMapper objectMapper = new ObjectMapper();

  public <T> T fromJson(String jsonValue, Class<T> clz) throws IOException {
    return objectMapper.readValue(jsonValue, clz);
  }

  public String toJson(Object obj) throws JsonProcessingException {
    return objectMapper.writeValueAsString(obj);
  }
}
